"""扫描记录Schema"""
from typing import Optional, Dict, Any
from pydantic import BaseModel, Field
from datetime import datetime


class ScanRecordBase(BaseModel):
    """扫描记录基础Schema"""
    task_id: int = Field(..., description="任务ID")
    branch: str = Field(..., description="扫描分支", max_length=255)
    scan_time: datetime = Field(..., description="扫描时间")
    status: str = Field(..., description="状态: success/failed/partial")
    duration: Optional[int] = Field(None, description="扫描耗时(秒)")
    scan_params: Optional[Dict[str, Any]] = Field(None, description="扫描参数")
    created_by: Optional[str] = Field(None, description="创建人", max_length=100)


class ScanRecordCreate(ScanRecordBase):
    """创建扫描记录Schema"""
    pass


class ScanRecordUpdate(BaseModel):
    """更新扫描记录Schema"""
    status: Optional[str] = Field(None, description="状态")
    duration: Optional[int] = Field(None, description="扫描耗时(秒)")
    issue_count: Optional[int] = Field(None, description="问题总数")
    confirmed_count: Optional[int] = Field(None, description="已确认问题数")
    unconfirmed_count: Optional[int] = Field(None, description="未确认问题数")


class ScanRecordResponse(ScanRecordBase):
    """扫描记录响应Schema"""
    id: int
    issue_count: int = 0
    confirmed_count: int = 0
    unconfirmed_count: int = 0
    created_at: datetime
    updated_at: datetime
    # 关联数据
    task_name: Optional[str] = None

    class Config:
        from_attributes = True

